package com.wlstock.funi.utils;

import android.content.Context;

import com.wlstock.funi.constant.SharePrefConstants;

import static com.wlstock.funi.utils.SharePreferenceUtils.getFloatSP;

/**
 * Created by admin on 16/7/31.
 */
public class GlobalIndexHelper {

    public static final  float LOWEST_ENV_FEEL = 0.15f;

    public static final  float HIGHEST_ENV_FEEL = 0.8f;

    public static final  float LOWEST_BUYER_INDEX = 0.3F;

    private static GlobalIndexHelper instance;
    private Context mContext;

    private GlobalIndexHelper(Context mContext) {
        this.mContext = mContext;
    }

    public static GlobalIndexHelper getInstance(Context mContex) {
        if (instance == null) {
            synchronized (GlobalIndexHelper.class) {
                if (instance == null) {
                    instance = new GlobalIndexHelper(mContex);
                }
            }
        }
        return instance;
    }


    public void setBuyPercent(Context mContext){
        float wlPCT = SharePreferenceUtils.getFloatSP(mContext, SharePrefConstants.MARKET_WL_KEEP_PCT,0.5f);
        float zf0PCT = SharePreferenceUtils.getFloatSP(mContext, SharePrefConstants.MARKET_0ZF_PCT, 0.5f);
        float zf3PCT = SharePreferenceUtils.getFloatSP(mContext, SharePrefConstants.MARKET_3ZF_PCT, 0.5f);
        float zf10PCT = SharePreferenceUtils.getFloatSP(mContext, SharePrefConstants.MARKET_10ZF_PCT, 0.5f);
        float yMPCT = SharePreferenceUtils.getFloatSP(mContext, SharePrefConstants.MARKET_YIMENG_PCT, 0.5f);
        //下面的都是权
        double value = 0.2 * wlPCT + 0.2 * zf10PCT + 0.3 * zf3PCT + 0.2 * zf0PCT + 0.1 * yMPCT;
        SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_FINAL_PCT, (float)value);
    }

    /**
     * 计算 持仓系数,大于0.5的时候表示可以买入,小于0.5表示不是很建议买入
     * @return
     */
    public float getBuyPercent(){
        return getFloatSP(mContext, SharePrefConstants.MARKET_FINAL_PCT, -1f);
    }


    /**
     * 设置存储涨幅大于3%的占比
     */
    public void set3ZFPCT(){
        int zf3 = SharePreferenceUtils.getIntSP(mContext, SharePrefConstants.COUNT_ZFHIGHERTHAN3,-1);
        int df3 = SharePreferenceUtils.getIntSP(mContext, SharePrefConstants.COUNT_DFHIGHERTHAN3,-1);
        if (zf3 == -1 || df3 == -1) return;
        SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_3ZF_PCT, ((float) zf3)/(zf3+df3));
    }

    /**
     * 设置涨幅大于10%的占比
     */

    public void set10ZFPCT(){
        int zf10 = SharePreferenceUtils.getIntSP(mContext, SharePrefConstants.COUNT_ZFHIGHERTHAN10,-1);
        int df10 = SharePreferenceUtils.getIntSP(mContext, SharePrefConstants.COUNT_DFHIGHERTHAN10,-1);
        if (zf10 == -1 || df10 == -1) return;
        SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_10ZF_PCT, ((float)zf10)/(zf10+df10));
    }


    public boolean couldBuy() {
        return getBuyPercent()>GlobalIndexHelper.LOWEST_BUYER_INDEX;
    }


    /**
     * 根据市场情绪获取现在期待的仓位
     * @return
     */
    public float getExptectPosition() {
        float buyPercent = getBuyPercent();
        if ( buyPercent < GlobalIndexHelper.LOWEST_BUYER_INDEX ){
            return 0.01f;
        }else {
            if (buyPercent > 0.7)
                return 1;
            else
                return buyPercent;
        }
    }
}
